package com.st.emps.staff.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.st.emps.staff.model.StaffModel;
import com.st.emps.staff.service.StaffService;
import com.st.emps.util.FmtRequest;
import com.st.emps.util.Upload;

/**
 * Servlet implementation class StaffServlet
 */
@WebServlet("/StaffServlet")
public class StaffServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StaffServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String action = request.getParameter("action");
		Object rs = null;
		switch (action) {
		case "reg":
			rs = register(request);
			break;
		case "login":
			rs = login(request);
			break;
		case "add":
			rs = add(request);
			break;
		case "page":
			rs = page(request);
			break;
		case "del":
		    rs = delete(request);
		    break;
		case "upd":
		    rs = update(request);
		    break;
		case "get":
		    rs = get(request);
		    break;
		case "updPass":
			rs = updPass(request);
			break;
		case "resetPass":
			rs = resetPass(request);
			break;
		case "updDepart":
			rs = updDepart(request);
			break;
		case "delpic":
			rs = delPic(request);
			break;
		case "logout":
            request.getSession().removeAttribute("user");
            response.sendRedirect(request.getContextPath()+"/web/login.jsp");
			return ;
		default:
			break;
		}

		FmtRequest.write(response.getWriter(), rs);

	}



	private  String delPic(HttpServletRequest request) throws IOException {
		String code = request.getParameter("code");
		StaffModel model = new StaffModel(code);
		StaffModel personModel = service.selectModel(model);
		String image = personModel.getImage();
		Upload.delFile(image);
		personModel.setImage("");
		StaffModel personSession = (StaffModel) request.getSession().getAttribute("user");
		if (personSession.getCode().equals(code))
			request.getSession().setAttribute("user", personModel);
		model.setImage("");
		return service.updateImage(model);
	
	}


	private String updDepart(HttpServletRequest request) {
		Map<String,String> fields = new HashMap<String, String>();
		fields.put("code", "code");
		fields.put("departmentId", "codeDepart");
		StaffModel model = FmtRequest.parseModel(request, StaffModel.class, fields);
		return service.updateDepart(model);
	}

	
	private String resetPass(HttpServletRequest request) {
		StaffModel model = new StaffModel(request.getParameter("code"));
		return service.resetPassword(model);
	}

	private String updPass(HttpServletRequest request) {
		StaffModel model = new StaffModel(request.getParameter("code"),request.getParameter("password"));
		String s =service.updatePass(model,request.getParameter("password2"));
		return s;
	}

	private StaffService service = new StaffService();

	private Map<String,Object> page(HttpServletRequest request) {
		Map<String,String> fields = new HashMap<String, String>();
		fields.put("code", "code");
		fields.put("name", "name");
		StaffModel model = FmtRequest.parseModel(request, StaffModel.class,fields);
		model.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
		model.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
		model.setPageOn(true);
		Map<String,Object> map = new HashMap<>();
		map.put("list", service.selectList(model));
		map.put("count",service.selectCount(model));
		return map;
	}
	
	private String register(HttpServletRequest request) {
		StaffModel model = new StaffModel(request.getParameter("code"), request.getParameter("name"),request.getParameter("password"));
		return service.insert(model);
	}

	private String login(HttpServletRequest request) {
		StaffModel model = new StaffModel(request.getParameter("code"), request.getParameter("password"));
		String res =  service.login(model);
		if("1".equals(res)) {
			request.getSession().setAttribute("user", service.selectModel(model));
		}
		return res;
	}
	private String delete(HttpServletRequest request) {
		StaffModel model = new StaffModel(request.getParameter("code"));
		return service.delete(model);
	}

	private String update(HttpServletRequest request) {
		StaffModel model = FmtRequest.parseModel(request,StaffModel.class);
		String res =service.update(model);
		StaffModel user = (StaffModel) request.getSession().getAttribute("user");
		if("1".equals(res)&&user.getCode().equals(model.getCode())) {
			request.getSession().setAttribute("user", service.selectModel(model));
		}
		return res;
	}
	
	private StaffModel get(HttpServletRequest request) {
		StaffModel model = new StaffModel(request.getParameter("code"));
		return service.selectModel(model);
	}
//
//	private List<DepartmentModel> list(HttpServletRequest request) {
//		DepartmentModel model = FmtRequest.parseModel(request,DepartmentModel.class);
//		List<DepartmentModel> list = service.selectList(model);
//		return list;
//	}
	
	private String add(HttpServletRequest request) {
		StaffModel model = FmtRequest.parseModel(request,StaffModel.class);
		return service.insert(model);
	}

}
